// pages/consultationDetail/index.js
import {
    kbDocDetail,
    toggleCollectZ
} from '../../../apis/index';
Page({

    /**
     * 页面的初始数据
     */
    data: {

      hasCollect:false,
      info:{}
    },
    errorFun(){
        wx.showToast({
            icon:'none',
            title: '未能获取到资料',
            duration:2500,
            mask:true,
            success(){
                setTimeout(()=>{
                    wx.navigateBack({})
                },2500)
            }
        })
    },
    kbDovDetailFun(id){
        kbDocDetail(id).then(res=>{
            if(res.data.code == 200){
                wx.setNavigationBarTitle({
                    title: res.data.data.docTitle,
                })
                res.data.data.content =  this.formatRichText(res.data.data.content)
                this.setData({
                    info:res.data.data,
                    hasCollect:res.data.data.ifCollect
                })
            }else{
                this.errorFun()
            }
        })
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
         console.log(options)
         wx.showShareMenu({
            withShareTicket: true
          })
         let {vid} = options
         if(vid){
             this.kbDovDetailFun(vid)
         }else{
            this.errorFun()
         }
    },
    collectIt(){
        let hasCollect =! this.data.hasCollect
        let info = this.data.info
        toggleCollectZ({
            docId:info.docId,
             ifCollect:hasCollect
        }).then(res=>{
            if(res.data.code == 200){
                this.setData({
                    hasCollect
                })
            }
        })

    },
 
    formatRichText(html){
        let newContent= html.replace(/<img[^>]*>/gi,function(match,capture){
            match = match.replace(/style="[^"]+"/gi, '').replace(/style='[^']+'/gi, '');
          match = match.replace(/width="[^"]+"/gi, '').replace(/width='[^']+'/gi, '');
            match = match.replace(/height="[^"]+"/gi, '').replace(/height='[^']+'/gi, '');
         return match;
         });
         newContent = newContent.replace(/style="[^"]+"/gi,function(match,capture){
             match = match.replace(/width:[^;]+;/gi, 'max-width:100%;').replace(/width:[^;]+;/gi, 'max-width:100%;');
             return match;
         });
         newContent = newContent.replace(/<br[^>]*\/>/gi, '');
         newContent = newContent.replace(/\<img/gi, '<img style="max-width:100%;height:auto;display:block;margin:2px 0;"');
         return newContent;
       },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {

    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {
        return {
          title: this.data.info.docTitle,
          path: `pages/trainingVideo/consultationDetail/index?vid=${this.data.info.docId}`,
          success: (res) => {
            // 分享成功
          },
          fail: (res) => {
            // 分享失败
          }
        }
    }
})